package com.cyt.gen.service.impl;

import com.cyt.base.model.dto.ReturnResult;
import com.cyt.base.util.ErrorCode;
import com.cyt.gen.dao.GeneratorDao;
import com.cyt.gen.model.vo.GeneratorVo;
import com.cyt.gen.service.FormFieldService;
import com.cyt.gen.service.FormTableService;
import com.cyt.gen.service.GeneratorService;
import com.cyt.gen.utils.GeneratorActivitiUtil;
import com.cyt.gen.utils.GeneratorBaseUtil;
import com.cyt.gen.utils.GeneratorGeneralUtil;
import com.cyt.gen.utils.GeneratorUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 代码生成
 *
 * @author rong yuan
 * @date 2019/7/3 10:46
 */
@Service("generatorService")
public class GeneratorServiceImpl implements GeneratorService {

    /**
     * 1-后台代码；2-通用代码；3-工作流代码
     */
    private static final String GENERATOR_TYPE_BASE = "1";
    private static final String GENERATOR_TYPE_GENERAL = "2";
    private static final String GENERATOR_TYPE_ACTIVITI = "3";

    @Resource
    private GeneratorDao generatorDao;
    @Resource
    private FormTableService formTableService;
    @Resource
    private FormFieldService formFieldService;

    /**
     * 数据库表分页查询
     *
     * @param generatorVo 传入参数对象
     * @param pageNum     当前页数
     * @param pageSize    展示数量
     * @return ReturnResult
     * @author rong yuan
     * @date 2019/7/3 10:46
     */
    @Override
    public ReturnResult pageTable(GeneratorVo generatorVo, int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<GeneratorVo> list = generatorDao.listTable(generatorVo);
        PageInfo<GeneratorVo> page = new PageInfo<>(list);
        return new ReturnResult(page.getList(), page.getTotal(), page.isHasNextPage());
    }

    /**
     * 获取数据库表信息
     *
     * @param generatorVo 传入参数对象
     * @return GeneratorVo
     * @author rong yuan
     * @date 2019/7/3 11:02
     */
    @Override
    public GeneratorVo selectTable(GeneratorVo generatorVo) {
        generatorVo = generatorDao.selectTable(generatorVo);
        if (StringUtils.isNotBlank(generatorVo.getTableComment())) {
            String[] tableComments = generatorVo.getTableComment().replace("cyt_", "").split("-");
            generatorVo.setModuleLogName(tableComments[0]);
            if (tableComments.length > 1) {
                generatorVo.setClassLogName(tableComments[1]);
            }
        }
        if (StringUtils.isNotBlank(generatorVo.getTableName())) {
            String[] tableNames = generatorVo.getTableName().replace("cyt_", "").split("_");
            generatorVo.setPackageName(tableNames[0].toLowerCase());
            generatorVo.setModuleName(tableNames[0].toLowerCase());
            StringBuilder stringBuilder = new StringBuilder();
            for (String tableName : tableNames) {
                stringBuilder.append(Character.toUpperCase(tableName.charAt(0)));
                stringBuilder.append(tableName.toLowerCase().substring(1));
            }
            generatorVo.setEntityName(stringBuilder.toString());
        }
        return generatorVo;
    }

    /**
     * 生成代码
     *
     * @param generatorVo 传入参数对象
     * @return ReturnResult
     * @author rong yuan
     * @date 2019/7/3 11:09
     */
    @Override
    public ReturnResult generatorCode(GeneratorVo generatorVo) {
        try {
            GeneratorUtil generatorUtil;
            if (GENERATOR_TYPE_BASE.equals(generatorVo.getGeneratorType())) {
                generatorUtil = new GeneratorBaseUtil(generatorVo);
            } else if (GENERATOR_TYPE_GENERAL.equals(generatorVo.getGeneratorType())) {
                generatorUtil = new GeneratorGeneralUtil(generatorVo, formTableService, formFieldService);
            } else if (GENERATOR_TYPE_ACTIVITI.equals(generatorVo.getGeneratorType())) {
                generatorUtil = new GeneratorActivitiUtil(generatorVo, formTableService, formFieldService);
            } else {
                generatorUtil = new GeneratorUtil(generatorVo);
            }
            generatorUtil.generatorCode();
            return new ReturnResult(ErrorCode.CODE_0, "代码生成成功");
        } catch (Exception e) {
            return new ReturnResult(ErrorCode.CODE_1, "代码生成失败");
        }
    }
}